package com.example.beat_x3.Adapter;


import android.annotation.SuppressLint;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.beat_x3.Client.LoadImages;
import com.example.beat_x3.R;

import java.util.ArrayList;

import Entity.Equipment;
import Entity.Inf;


public class InstrumentAdapter extends RecyclerView.Adapter<InstrumentAdapter.Holder> {
    public ArrayList<Equipment> equipments;
    int old_size;
    private OnItemClickListener mOnItemClickListener1;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener1 = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(String equipmentNumber, int position, int span);
    }

    @NonNull
    @Override
    public InstrumentAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new InstrumentAdapter.Holder(LayoutInflater.from(Inf.context).inflate(R.layout.item_instrument, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull InstrumentAdapter.Holder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        if (equipments == null) return 0;
        return equipments.size();
    }

    public void appendNew() {
        if (getItemCount() > old_size) {
            notifyItemRangeChanged(old_size, getItemCount() - old_size);
            old_size = equipments.size();
        }
    }

    public void remove(int position) {
        equipments.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, getItemCount() - position);
        old_size = getItemCount();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void refresh() {
        if (equipments != null) {
            notifyDataSetChanged();
            old_size = getItemCount();
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView name, span_text, details;
        String instrumentId;
        int position1, span;

        public Holder(@NonNull View itemView) {
            super(itemView);
            this.imageView = itemView.findViewById(R.id.appCompatImageView2);
            this.name = itemView.findViewById(R.id.instrument_text1);
            this.span_text = itemView.findViewById(R.id.instrument_text3);
            this.details = itemView.findViewById(R.id.instrument_text2);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener1 != null) {
                        mOnItemClickListener1.onItemClick(instrumentId, position1, span);
                    }
                }
            });
        }

        @SuppressLint("SetTextI18n")
        void bind(int position) {
            position1 = position;
            span = equipments.get(position).Span;
            instrumentId = equipments.get(position).EquipmentNumber;
            name.setText(equipments.get(position).EquipmentName);
            span_text.setText("可借天数："+span + "天");
            details.setText("描述："+equipments.get(position).Detail);
            ViewGroup.LayoutParams params = imageView.getLayoutParams();
            imageView.setLayoutParams(params);
            if (Inf.equipment_images != null) {
                if (!Inf.equipment_images.containsKey(equipments.get(position).EquipmentImage)) {
                    LoadImages.downloadImages(LoadImages.EQUIPMENT_SRC, equipments.get(position).EquipmentImage);
                    }
            }
            imageView.setImageURI(Uri.parse(LoadImages.EQUIPMENT_SRC + "/" + equipments.get(position).EquipmentImage));
        }

    }
}
